import { routerRedux } from 'dva/router';
import * as apis from '../services/loginService'

export default {

    namespace: 'app',

    state: {
        logind: false,
        user: {},
        serverRes: {
            code: 0,
            msg: null
        },
        url: '/'
    },

    reducers: {
        initServerRes: initServerRes,
        setServerRes: setServerRes,
        setUser: setUser,
        setLogind: setLogind,
        setUserMechanismId: setUserMechanismId,
        setUrl: setUrl
    },

    effects: {
        login: login,
        register: register,
        login: login,
        appMenuReplacePage: appMenuReplacePage,
        deleteUser: deleteUser
    },

    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(location => {
                dispatch({
                    type: "setUrl",
                    data: location.pathname
                })
            });
        }

    }
}

function setUrl(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.url = payload.data
    return _state
}

/**
 * 机构创建成功后为用户添加机构id
 */
function setUserMechanismId(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.user.mechanismId = payload.data.mechanismId
    return _state
}

/**
 * 修改登录状态
 */
function setLogind(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.logind = payload.data
    return _state
}

/**
 * 写入服务器返回信息
 */
function setServerRes(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    if (payload.data == undefined || payload.data == "") {
        return _state
    }
    _state.serverRes = payload.data
    return _state;
}

/**
 * 添加用户数据
 */
function setUser(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.user = payload.data
    return _state
}

/**
 * 初始化服务器返回信息
 */
function initServerRes(state, payload) {
    let _state = JSON.parse(JSON.stringify(state));
    _state.serverRes = {
        code: 0,
        message: null
    }
    return _state
}

/**
 * 用户登录
 */
function* login(payload, { select, call, put }) {
    let loginData = payload.data
    let result = yield call(apis.login, loginData)
    if (result.data.code == 200) {
        yield put({
            type: 'app/setLogind',
            data: true
        })
        yield put({
            type: 'app/setUser',
            data: result.data.data
        })
    }

    yield put({
        type: 'app/setServerRes',
        data: result.data
    })

    yield put({
        type: 'app/initServerRes',
    })
}

/**
 * 菜单栏页面跳转
 */
function* appMenuReplacePage(payload, { select, call, put }) {
    yield put(routerRedux.replace(payload.data))
}


/**
 * 用户注册 
 */
function* register(payload, { select, call, put }) {
    let registerData = payload.data
    let result = yield call(apis.register, registerData)
    yield put({
        type: 'app/setServerRes',
        data: result.data
    })
}

/**
 * 注销用户账号
 */
function* deleteUser(payload, { select, call, put }) {
    let result = yield call(apis.deleteUser, payload.data)

    yield put({
        type: 'app/setServerRes',
        data: result.data
    })

    yield put({
        type: "backstageManagement/getUserTableData"
    })
}



